<?php

/*
 * Class designed to manage match planning
 */

class Match {

  /*
   * Unique id of the match
   */

  public $id = null;

  /*
   * Date and time of the event
   */

  public $eventDate = null;

  /*
   * Location of thye event
   */

  public $location = null;

  /*
   * Category of the participating teams
   */

  public $category = null;

  /*
   * Description of the event
   */

  public $descript = null;


  public function __construct( $data=array() ) {
    if ( isset( $data['id'] ) ) $this->id = (int)$data['id'];
    if ( isset( $data['eventDate'] ) ) $this->eventDate = $data['eventDate'];
	if ( isset( $data['location'] ) ) $this->location = $data['location'];
	if ( isset( $data['category'] ) ) $this->category = $data['category'];
    if ( isset( $data['descript'] ) ) $this->descript = $data['descript'];
	}


  public function storeFormValues( $params ) {
	  $this->__construct( $params );
  }


  public static function getById( $id ) {
	  $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT * FROM matches WHERE id = :id";
    $st = $conn->prepare( $sql );
    $st->bindValue( ":id", $id, PDO::PARAM_INT );
    $st->execute();
    $row = $st->fetch();
    $conn = null;
    if ( $row ) return new Match( $row );
	}


  public static function getByCategory( $category ) {
	  $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT * FROM matches WHERE category=:category
                                  ORDER BY eventDate";
    $st = $conn->prepare( $sql );
    $st->bindValue( ":category", $category, PDO::PARAM_STR );
    $st->execute();
    $list = array();

	  while ( $row = $st->fetch() ) {
	    $match = new Match( $row );
	    $list[] = $match;
    }

    $conn = null;
    return ( array ( "results" => $list ) );
	}


  public static function getList( $numRows=1000000) {
      $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
      $sql = "SELECT * FROM matches ORDER BY eventDate";

      $st = $conn->prepare( $sql );
      //$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
      $st->execute();
      $list = array();

      while ( $row = $st->fetch() ) {
        $match = new Match( $row );
        $list[] = $match;
      }

      // Now get the total number of Match that matched the criteria
      $sql = "SELECT FOUND_ROWS() AS totalRows";
      $totalRows = $conn->query( $sql )->fetch();
      $conn = null;
      return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );

	}


  public function insert() {

	    // Does the Match object already have an ID?
	    if ( !is_null( $this->id ) ) trigger_error ( "Match::insert(): Attempt to insert an Match object that already has its ID property set (to $this->id).", E_USER_ERROR );

	    // Insert the Match
	    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
	    $this->id = $conn->lastInsertId();
	    $sql = "INSERT INTO matches ( id, eventDate, location, category, descript ) VALUES ( :id, FROM_UNIXTIME(:eventDate), :location, :category, :descript )";
	    $st = $conn->prepare ( $sql );
	    $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
        $st->bindValue( ":eventDate", $this->eventDate, PDO::PARAM_INT );
	    $st->bindValue( ":location", $this->location, PDO::PARAM_STR );
	    $st->bindValue( ":category", $this->category, PDO::PARAM_STR );
        $st->bindValue( ":descript", $this->descript, PDO::PARAM_STR );
	    $st->execute();
	    $conn = null;
    }


  public function update() {
      if ( is_null( $this->id ) ) trigger_error ( "Match::update(): Attempt to update an match object that does not have its ID property set.", E_USER_ERROR );

      $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
      $sql = "UPDATE matches SET eventDate=FROM_UNIXTIME(:eventDate), location=:location, category=:category, descript=:descript WHERE id = :id";
      $st = $conn->prepare ( $sql );
      $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
      $st->bindValue( ":eventDate", $this->eventDate, PDO::PARAM_INT );
	  $st->bindValue( ":location", $this->location, PDO::PARAM_STR );
	  $st->bindValue( ":category", $this->category, PDO::PARAM_STR );
      $st->bindValue( ":descript", $this->descript, PDO::PARAM_STR );
      $st->execute();
      $conn = null;
    }


  public function delete() {
    if ( is_null( $this->id ) ) trigger_error ( "Match::delete(): Attempt to delete an match object that does not have an ID property.", E_USER_ERROR );

    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "DELETE FROM matches WHERE id=:id LIMIT 1";
    $st = $conn->prepare( $sql );
    $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
    $st->execute();
    $conn = null;
  }

}
?>
